import {createRouter, createWebHistory} from 'vue-router'
import Layout from '../layout/Layout.vue'

const routes = [
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        redirect: "/home",
        children: [
            {
                path: 'home',
                name: 'Home',
                component: () => import("@/views/Home"),
            },
            {
                path: 'user',
                name: 'User',
                component: () => import("@/views/User"),
            },
            {
                path: 'goods',
                name: 'Goods',
                component: () => import("@/views/Goods"),
            },
            {
                path: 'news',
                name: 'News',
                component: () => import("@/views/News"),
            },
            {
                path: 'person',
                name: 'Person',
                component: () => import("@/views/Person"),
            },
            {
                path: 'category',
                name: 'Category',
                component: () => import("@/views/Category"),
            },
            {
                path: 'map',
                name: 'Map',
                component: () => import("@/views/Map"),
            },
            {
                path: 'im',
                name: 'Im',
                component: () => import("@/views/Im"),
            },
            {
                path: 'message',
                name: 'Message',
                component: () => import("@/views/Message"),
            },
            {
                path: 'donate',
                name: 'Donate',
                component: () => import("@/views/Donate"),
            },
            {
                path: 'order',
                name: 'Order',
                component: () => import("@/views/Order"),
            },
            {
                path: 'permission',
                name: 'Permission',
                component: () => import("@/views/Permission"),
            },
            {
                path: 'calendar',
                name: 'Calendar',
                component: () => import("@/views/Calendar"),
            },
            {
                path: 'place',
                name: 'Place',
                component: () => import("@/views/Place"),
            },
            {
                path: 'userorder',
                name: 'UserOrder',
                component: () => import("@/views/UserOrder"),
            },
            {
                path: 'procurement',
                name: 'Procurement',
                component: () => import("@/views/procurement"),
            },
            {
                path: 'rolling',
                name: 'Rolling',
                component: () => import("@/views/Rolling"),
            },
            {
                path: 'enquiry',
                name: 'Enquiry',
                component: () => import("@/views/Enquiry"),
            },
            {
                path: 'busnews',
                name: 'Busnews',
                component: () => import("@/views/Busnews"),
            }
        ]
    },
    {
        path: '/about',
        name: 'about',
        component: () => import("@/views/User")
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import("@/views/Login")
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import("@/views/Register")
    },
    {
        path: '/show',
        name: 'Show',
        component: () => import("@/views/Show")
    },
    {
        path: '/front',
        name: 'Front',
        component: () => import("@/views/front/Front"),
        children: [
            {
                path: 'home',
                name: 'frontHome',
                component: () => import("@/views/front/Home"),
            },
            {
                path: 'news',
                name: 'frontNews',
                component: () => import("@/views/front/News"),
            },
            {
                path: 'analyse',
                name: 'frontAnalyse',
                component: () => import("@/views/front/Analyse"),
            },
            {
                path: 'item1',
                name: 'Item1',
                component: () => import('../views/front/Item1.vue')
            },
            {
                path: 'item2',
                name: 'Item2',
                component: () => import('../views/front/Item2.vue')
            },
            {
                path: 'itempage1',
                name: 'Itempage1',
                component: () => import('../views/front/Itempage1.vue')
            },
            {
                path: 'itempage2',
                name: 'Itempage2',
                component: () => import('../views/front/Itempage2.vue')
            },
            {
                path: 'itempage3',
                name: 'Itempage3',
                component: () => import('../views/front/Itempage3.vue')
            },
            {
                path: 'BusNews',
                name: 'frontBusNews',
                component: () => import('../views/front/BusNews.vue')
            },
            {
                path: 'Merchants',
                name: 'frontMerchants',
                component: () => import('../views/front/Merchants.vue')
            },
            {
                path: 'Message',
                name: 'frontMessage',
                component: () => import('../views/front/Message.vue')
            },
            {
                path: 'Place',
                name: 'frontPlace',
                component: () => import('../views/front/Place.vue')
            },
            {
                path: 'Goods',
                name: 'frontGoods',
                component: () => import('../views/front/Goods.vue')
            },
            {
                path: 'Bususer',
                name: 'frontBususer',
                component: () => import('../views/front/Bususer.vue')
            }
        ]
    },
    {
        path: '/business',
        name: 'Business',
        component: () => import("@/views/business/Front"),
        children: [
            {
                path: 'home',
                name: 'BusinessHome',
                component: () => import("@/views/business/Home"),
            },
            {
                path: 'person',
                name: 'BusinessPerson',
                component: () => import("@/views/business/Person"),
            },
            {
                path: 'busnews',
                name: 'BusinessBusnews',
                component: () => import("@/views/business/Busnews"),
            },
            {
                path: 'calendar',
                name: 'BusinessCalendar',
                component: () => import("@/views/business/Calendar"),
            },
            {
                path: 'busgoods',
                name: 'BusinessBusgoods',
                component: () => import("@/views/business/Busgoods"),
            },
            {
                path: 'place',
                name: 'BusinessPlace',
                component: () => import("@/views/business/Place"),
            },
            {
                path: 'order',
                name: 'BusinessOrder',
                component: () => import("@/views/business/Order"),
            },
            {
                path: 'procurement',
                name: 'BusinessProcurement',
                component: () => import("@/views/business/Procurement"),
            },
            {
                path: 'sprocurement',
                name: 'BusinessSprocurement',
                component: () => import("@/views/business/Sprocurement"),
            },
            {
                path: 'rolling',
                name: 'BusinessRolling',
                component: () => import("@/views/business/Rolling"),
            },
            {
                path: 'enquiry',
                name: 'BusinessEnquiry',
                component: () => import("@/views/business/Enquiry"),
            }

        ]
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 限制某些页面禁止未登录访问
let limitPagePath = ['/about']

//登录信息判断
router.beforeEach((to, from, next) => {
    if (limitPagePath.includes(to.path)) {
        // 判断sessionStorage是否保存了用户信息
        let userStr = sessionStorage.getItem("user") || "{}"
        let user = JSON.parse(userStr)
        if (!user.id) {
            // 跳转到登录页面
            next({path: "/login"})
        } else {
            next()
        }
    } else {
        next()
    }

})

export default router
